package me.kafeitu.activiti.chapter11;

import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.test.PluggableActivitiTestCase;
import org.activiti.engine.management.TablePage;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.junit.Test;

import me.kafeitu.activiti.AbstractTest;
import me.kafeitu.activiti.AbstractTestMysql;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 手动触发消息
 *
 * @author henryyan
 */
public class MessageBoundaryEventTest4 extends AbstractTestMysql {
    
    /**
     * deploy
     * @throws Exception
     */
    // @Test
    public void testClasspathDeployment() throws Exception {
        // 定义classpath 
        String bpmnClasspath = "chapter11/boundaryEvent/messageBoundaryEventNoCancelActivity4.bpmn";
        // 添加资源
        repositoryService.createDeployment().addClasspathResource(bpmnClasspath).deploy();
    }
    
    /**
     * clean deployment
     */
    //@Test
    public void testDelete() throws Exception {
        List<org.activiti.engine.repository.Deployment> list = repositoryService.createDeploymentQuery()
                .processDefinitionKey("messageBoundaryEventNoCancelActivity").list();
        for (org.activiti.engine.repository.Deployment dep:list) {
            String deploymentId = dep.getId();
            repositoryService.deleteDeployment(deploymentId, true);
        }
    }
    
    
    
    /**
     * 触发多次消息事件--cancelActivity='false'
     */
    @Test
    // @Deployment(resources = "chapter11/boundaryEvent/messageBoundaryEventNoCancelActivity4.bpmn")
    public void testTriggerManyTimes() throws Exception {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "xian1");
        variables.put("shiLeader", "shiLeader1");
        
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("messageBoundaryEventNoCancelActivity", variables);
        assertNotNull(processInstance);

        System.out.println("11eid=" + processInstance.getId() + ", pid=" + processInstance.getProcessInstanceId());

        // 审核文件任务
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery().messageEventSubscriptionName("MSG_协助处理");
        Execution execution = executionQuery.singleResult();

        // 触发两次
        Map<String, Object> variables2 = new HashMap<String, Object>();
        variables2.put("user", "xian2");
        runtimeService.messageEventReceived("MSG_协助处理", execution.getId(), variables2);
        Map<String, Object> variables3 = new HashMap<String, Object>();
        variables3.put("user", "xian3");
        runtimeService.messageEventReceived("MSG_协助处理", execution.getId(), variables3);


        // 任务到达“县申请”
        List<Task> list = taskService.createTaskQuery().taskName("县申请").list();

        // 完“县申请”处理任务
        for (Task task2 : list) {
            Map<String, Object> variables11 = new HashMap<String, Object>();
            variables11.put("leader", "leader1");
            taskService.complete(task2.getId(), variables11);
        }
        
        // 先进2个
        List<Task> list4 = taskService.createTaskQuery().taskName("县审核").list();
        
        Map<String, Object> variables8 = new HashMap<String, Object>();
        variables8.put("pass", false);
        taskService.complete(list4.get(0).getId(), variables8);
        taskService.complete(list4.get(2).getId(), variables8);

        System.out.println("end");
    }
    
    
   // @Test
    public void testShi() throws Exception {
        
        List<Task> list = taskService.createTaskQuery().taskName("市汇总").list();
        
        for (Task task2 : list) {
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("shipass", false);
            //variables.put("shengLeader", "shengLeader1");
            taskService.complete(task2.getId(), variables);
        }

    }
    
    // @Test
    public void testSheng() throws Exception {
        
        List<Task> list = taskService.createTaskQuery().taskName("省").list();
        
        for (Task task2 : list) {
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("shengPass", false);
            // variables.put("shengLeader", "shengLeader1");
            taskService.complete(task2.getId(), variables);
        }
    }

}
